clear all
set more off, perm
global cd "/mnt/brownresearch/ECON_LaPorta/rlaporta/research/BGLS2023"
global Figures $cd/TS/Figures
global WRDS    $cd/WRDS

* uses same data as TS:
global Data   $cd/TS/DataForTables 
* ************************************************
* compound risk free rate
use $Data/risk_free.dta, clear
rename t30ret rf1mo
gen rf1t=1
gen rf3t=1
gen rf5t=1
tsset datem
forvalues x=1(1)12 {
	quietly: replace rf1t=rf1t*(1+F`x'.rf1mo) 
}
forvalues x=1(1)36 {
	quietly: replace rf3t=rf3t*(1+F`x'.rf1mo) 
}
forvalues x=1(1)60 {
	quietly: replace rf5t=rf5t*(1+F`x'.rf1mo) 
}
keep datem rf1t rf3t rf5t rf1mo
tempfile rf
save "`rf'", replace
* ************************************************
import sas caldt totval spindx vwretd vwretx sprtrn using $WRDS/CRSP/msp500.sas7bdat, clear
keep if spindx<.

rename caldt date
format date %td

gen datem=mofd(date)
format datem %tm
tsset datem

gen RetYr1=1
gen RetYr3=1
gen RetYr5=1

forvalues x=1(1)12 {
	replace RetYr1=RetYr1*(1+F`x'.sprtrn)
}
forvalues x=1(1)36 {
	replace RetYr3=RetYr3*(1+F`x'.sprtrn)
}
forvalues x=1(1)60 {
	replace RetYr5=RetYr5*(1+F`x'.sprtrn)
}
* ADD RISK FrEE
merge 1:1 datem using "`rf'"
keep if _merge==3
drop _merge

generate xRet1Yr=RetYr1-rf1t
generate xRet3Yr=RetYr3-rf3t 
generate xRet5Yr=RetYr5-rf5t	

tempfile tmp
save "`tmp'", replace
* ************************************************************************
use if LTG<. & dofm(datem)<mdy(12,31,2020) using $Data/TimeSeries01, clear
merge 1:1 datem using "`tmp'"
keep if _merge==3
drop _merge

* winsorize the data
foreach var of varlist LTG STGE1 STGE2 e_cae3   {
	quietly: bysort datem: egen max=pctile(`var'), p(99)
	quietly: bysort datem: egen min=pctile(`var'), p(1)  
	quietly: replace `var'=max if `var'>max & `var'<.
	quietly: replace `var'=min if `var'<min	
	drop max min 
}
label var LTG "LTG"
label var STGE1 "E{sub:t}{sup:O}[e{sub:t+1}]-e{sub:t}"
tsset datem
local min=mofd(mdy(12,31,1981))
local kin=mofd(mdy(12,31,1985))
local max=mofd(mdy(12,31,2020))
display `min'
display `max'
twoway	(tsline LTG	, lcolor(red))					/*
*/	(tsline STGE1	, lcolor(green) yaxis(2)), 			/*
*/	ylabel(, format(%4.1f)) ylabel(, format(%4.1f) axis(2))		/*
*/	xtitle("")							/*
*/	legend(off)							/*			
*/ 	scheme(s1color)			
*/	tlabel(`min' `kin'(60)`max',labsize(vsmall) format(%tmCY))
graph export $Figures/Figure1.tif, replace
* **********
preserve
gen pd=-dp
keep if r5<. & dofm(datem)<=mdy(12,31,2015)
foreach var of varlist LTG r5 pd   {
	egen `var'std=std(`var')
	replace `var'=`var'std
	drop `var'std
}
local min=mofd(mdy(12,31,1981))
local kin=mofd(mdy(12,31,1985))
local max=mofd(mdy(12,31,2015))
twoway	(tsline r5 , lcolor(black))					/*
*/	(tsline LTG, lcolor(red)) 					/*
*/	(tsline pd , lcolor(green)),					/*
*/	ytitle("")							/*
*/	xtitle("")							/*
*/	tlabel(`min' `kin'(60)`max',labsize(vsmall) format(%tmCY))	/*
*/	legend(off)							/*			
*/ 	scheme(s1color)			
/*	legend(order(1 "r{sub:t,t+5}" 2 "LTG" 3 "pd") cols(3)) 		*/	
graph export $Figures/Figure2.tif, replace
* *********
restore
